package com.haisen.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentGetRequest;
import com.dingtalk.api.request.OapiDepartmentListIdsRequest;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.response.OapiDepartmentGetResponse;
import com.dingtalk.api.response.OapiDepartmentListIdsResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.haisen.exception.BizException;
import com.haisen.service.DingDepartmentService;
import com.haisen.service.DingTokenServcie;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DingDepartmentServiceImpl implements DingDepartmentService {

     private String accessToken = "";

     @Autowired
     private DingTokenServcie dingTokenServcie;
    /**
     * 获取部门列表，如果pid不传，默认为根的部门
     * @return
     */
    @Override
    public List<OapiDepartmentListResponse.Department> getDepartments(String pid) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
         request.setId(pid);
         request.setFetchChild(true);
        request.setHttpMethod("GET");
        try{
            OapiDepartmentListResponse response = client.execute(request, accessToken);
            if ( response.getErrcode() !=0){
                throw  new BizException("获取部门列表失败"+response.getErrmsg());
            }
            return response.getDepartment();
        } catch (ApiException exception){
            throw  new BizException("请求部门列表详情失败"+exception);
        }


    }

    /**
     * 获取子部门，根部门则为1
     * @param pid
     * @return
     */
    @Override
    public List<Long> getSubDepartIds(String pid) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        log.info("accesstoken={}",accessToken);
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list_ids");
        OapiDepartmentListIdsRequest request = new OapiDepartmentListIdsRequest();
        request.setId(pid);
        request.setHttpMethod("GET");
        try {
            OapiDepartmentListIdsResponse response = client.execute(request, accessToken);
            if (response.getErrcode() !=0 ){
                throw  new BizException("获取部门结果失败"+response.getErrmsg());
            }
            return response.getSubDeptIdList();
        } catch (ApiException exception){
            throw  new BizException("请求部门列表失败"+exception.getErrMsg());
        }
    }

    /**
     * 根据部门id获取部门主管和管理者信息等
     * @param id
     * @return
     */
    @Override
    public OapiDepartmentGetResponse departmentGetResponse(String id) {
        accessToken = dingTokenServcie.load(DingTokenServiceImpl.TOKEN_KEY).getToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/get");
        OapiDepartmentGetRequest request = new OapiDepartmentGetRequest();
        request.setId(id);
        request.setHttpMethod("GET");
        try {
            OapiDepartmentGetResponse response = client.execute(request, accessToken);
            if (response.getErrcode() != 0){
                throw  new BizException("获取部门详情失败！"+response.getErrmsg());
            }
            return response;
        } catch (ApiException exception){
            throw  new BizException("请求部门详情失败!"+exception);
        }

    }
}
